<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 客诉系统
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-04-14
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ServiceController extends AdminController {
	
	/**
	 * @todo 客诉列表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function index(){
		//接参
		$activity_id = (int)I('activity_id',0);
		$merchant_id = (int)I('merchant_id',0);
		$shop_id = (int)I('shop_id',0);
		$customer_service_type = (int)I('customer_service_type',0);
		$customer_service_status = (int)I('customer_service_status',0);
		$uid = (int)I('uid',0);
		$customer_service_name = trim((string)strip_tags(I('customer_service_name')));//搜索内容
		$customer_service_mobile = trim((string)strip_tags(I('customer_service_mobile')));//搜索内容
		$customer_service_order_num = trim((string)strip_tags(I('customer_service_order_num')));//搜索内容
		$customer_service_content = trim((string)strip_tags(I('customer_service_content')));//搜索内容
		$start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
		$end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
		//初始化查询条件
		$map = array();
		if(!empty($customer_service_name)){
			$map['customer_service_name'] = array('like', '%'.$customer_service_name.'%');
		}
		if(!empty($customer_service_mobile)){
			$map['customer_service_mobile'] = array('like', '%'.$customer_service_mobile.'%');
		}
		if(!empty($customer_service_order_num)){
			$map['customer_service_order_num'] = array('like', '%'.$customer_service_order_num.'%');
		}
		if(!empty($customer_service_content)){
			$map['_string'] = '(customer_service_content like "%'.$customer_service_content.'%") OR ( customer_service_title like "%'.$customer_service_content.'%") ';
		}
		//activity_id可能为负数,线上..特卖..
		if($activity_id != 0){
			$map['activity_id'] = $activity_id;
		}
		if($merchant_id > 0){
			$map['merchant_id'] = $merchant_id;
		}
		if($shop_id > 0){
			$map['shop_id'] = $shop_id;
		}
		if($customer_service_type > 0){
			$map['customer_service_type'] = $customer_service_type;
		}
		if($customer_service_status > 0){
			$map['customer_service_status'] = $customer_service_status;
		}
		if($uid > 0){
			$map['uid'] = $uid;
		}
		$map['create_time'] = array('between', array($start_time, $end_time));
		$map['status'] = array('gt',-1);
		$list = $this->lists('customer_service',$map,'customer_service_id desc');
		//所属活动商户店铺
		$activity = D('Activity','Service')->getActivityList(0);//活动列表
		$activity_list = !empty($activity) ? $activity : null ;
		$obj = D('Admin/Shop');
		$merchant_list = $activity_id != 0 ? $obj->getActivityMerchant($activity_id) : null;  
		$shop_list = $activity_id != 0 && $merchant_id > 0 ? $obj->getMerchantShop($activity_id,$merchant_id) : null;
		//选择处理人
		$object = D('ServiceApi');
		$member_list = $object->getDepartmentMember(1);
	    if(false === $member_list){
	    	$this->error($object->getError()); 
	    }
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		
		//渲染
		$this->assign('meta_title','客诉管理');
		$this->assign('login_id' ,UID);
		$this->assign('activity_list',$activity_list);
		$this->assign('merchant_list',$merchant_list);
		$this->assign('shop_list',$shop_list);
		$this->assign('service_type',C('CUSTOMER_SERVICE_TYPE'));
		$this->assign('service_status',C('CUSTOMER_SERVICE_STATUS'));
		$this->assign('member_list',$member_list);
		$this->assign('_list', $list);
		$this->display();
	}
	
	/**
	 * @todo 新增客诉
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function add(){
		if(IS_POST){
			$this->update();
		}else{
			$activity = D('Activity','Service')->getActivityList(0);//活动列表
			$activity_list = !empty($activity) ? $activity : null ;
			$this->assign('activity_list',$activity_list);
			$this->assign('merchant_list',null);
			$this->assign('shop_list',null);
			$this->assign('info',null);
			$this->assign('meta_title','新增客诉');
			$this->assign('meta_title_c','客诉列表');
			$this->display();
		}
	}
	
	/**
	 * @todo 添加/编辑文章标签
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	private function update(){
		$customer_service_id = (int)I('customer_service_id',0);
		$obj = D('Admin/CustomerService');
		$id = $obj->update();
		if($id === false){
			$this->error($obj->getError());
		}else{
			if($customer_service_id == 0){ 
				action_log('service_add','CustomerService',$id,UID);
			}else{
				action_log('service_edit','CustomerService',$customer_service_id,UID);
			}
			$this->success('操作成功',Cookie('__forward__'));
		}
	}
	
	/**
	 * @todo 编辑客诉
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function edit(){
		if(IS_POST) {
			$this->update();
		} else {
			$customer_service_id = (int)I('id');
			if(0 > $customer_service_id){
				$this->error('参数传递错误');
			}
			$info = M('CustomerService')->where(array('customer_service_id'=>$customer_service_id))->find();
			if(empty($info)){
				$this->error('客诉信息不存在');
			}
			//基础信息
			$activity = D('Activity','Service')->getActivityList(0);
			$activity_list = !empty($activity) ? $activity : null ;
			$merchant_list = D('Admin/Shop')->getActivityMerchant($info['activity_id']);
			if(empty($merchant_list)){
				$merchant_list = null;
			}
			$shop_list = D('Admin/Shop')->getMerchantShop($info['activity_id'],$info['merchant_id']);
			if(empty($shop_list)){
				$shop_list = null;
			}
			//渲染
			$this->assign('activity_list',$activity_list);
			$this->assign('merchant_list',$merchant_list);
			$this->assign('shop_list',$shop_list);
			$this->assign('info',$info);
			$this->assign('meta_title','编辑客诉');
			$this->assign('meta_title_c','客诉列表');
			$this->display('add');
		}
	}
	
	/**
	 * @todo 删除客诉
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function change($method = null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$map[D('CustomerService')->getPk()] = array('in',$id);
		switch (strtolower($method) ){
			case 'forbid':
				$this->forbid('CustomerService', $map);
				break;
			case 'resume':
				$this->resume('CustomerService', $map);
				break;
			case 'delete':
				action_log('service_del', 'CustomerService', $id, UID);
				$this->delete('CustomerService', $map);
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * @todo 客诉报表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function handle(){
		$checked = (int)I('checked',0);
		$start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
		$end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
		$map = array();
		//点击搜索才给数据
		if(0 < $checked){
			$map['create_time'] = array('between', array($start_time, $end_time));
			$map['status'] = array('gt',-1);
		}
		//活动列表
		$activity = D('Activity','Service')->getActivityList(0);
		$activity_list = !empty($activity) ? $activity : null ;
		$obj = D('CustomerService');
		$field = 'customer_service_type,customer_service_status,uid';
		//取客诉数据
		$customer_data = $obj->getCustomerComplain($map,$field);
		//渲染
		$this->assign('meta_title','处理客诉报表');
		$this->assign('data',$customer_data);
		$this->assign('activity_list',$activity_list);
		$this->assign('service_type',C('CUSTOMER_SERVICE_TYPE'));
		$this->assign('service_status',C('CUSTOMER_SERVICE_STATUS'));
		$this->display();
		
	}
	
	/**
	 * @todo 店铺报表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function shop(){
		$activity_id = (int)I('activity_id',0);
		$merchant_id = (int)I('merchant_id',0);
		$shop_id = (int)I('shop_id',0);
		$start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
		$end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
		$map = array();
		//必须选活动,活动id可能为负数   天天特卖...
		if(0 != $activity_id){
			$map['activity_id'] = $activity_id;
			$map['customer_service_type'] = array('in','1,2,3,4,5,6,7,8,9,12');
			$map['create_time'] = array('between', array($start_time, $end_time));
			$map['status'] = array('gt',-1);
			if(0 < $merchant_id){
				$map['merchant_id'] = $merchant_id;
			}
			if(0 < $shop_id){
				$map['shop_id'] = $shop_id;
			}
		}
		//活动列表
		$activity = D('Activity','Service')->getActivityList(0);
		$activity_list = !empty($activity) ? $activity : null ;
		$merchant_list = $activity_id != 0 ? D('Admin/Shop')->getActivityMerchant($activity_id):null;
		$shop_list = $activity_id != 0 && $merchant_id > 0 ? D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id) : null;
		//取客诉数据
		$obj = D('CustomerService');
		$field = 'customer_service_id,customer_service_type,merchant_id,shop_id';
		$customer_data = $obj->getShopComplain($map,$field);
		//渲染
		$this->assign('meta_title','店铺客诉报表');
		$this->assign('data',$customer_data);
		$this->assign('activity_list',$activity_list);
		$this->assign('merchant_list',$merchant_list);
		$this->assign('shop_list',$shop_list);
		$this->assign('service_type',C('CUSTOMER_SERVICE_TYPE'));
		$this->display();
	}
	
	/**
	 * @todo 公司报表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function company(){
		$activity_id = (int)I('get.activity_id',0);
		$start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
		$end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
		$map = array();
		//必须选活动,活动id可能为负数   天天特卖...
		if(0 != $activity_id){
			$map['activity_id'] = $activity_id;
			$map['status'] = array('gt',-1);
			$map['customer_service_type'] = array('in','13,14,15,16,17');
			$map['create_time'] = array('between', array($start_time, $end_time));
		}
		//活动列表
		$activity = D('Activity','Service')->getActivityList(0);
		$activity_list = !empty($activity) ? $activity : null ;
		//取客诉数据
		$obj = D('CustomerService');
		$field = 'customer_service_id,customer_service_type';
		$customer_data = $obj->getCompanyComplain($map,$field);
		//渲染
		$this->assign('meta_title','公司客诉报表');
		$this->assign('data',$customer_data);
		$this->assign('activity_list',$activity_list);
		$this->assign('service_type',C('CUSTOMER_SERVICE_TYPE'));
		$this->display();
	}
	
}